package com.css.crm.vo;

import java.util.List;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.css.crm.cache.CrmCache;
import com.css.crm.utility.PlaceUtil;

@XmlRootElement(name = "customer-address")
public class CustomerAddressVo extends AuditTrailVo {

	/** CUST_ADDR_ID **/
	private Long custAddrId;
	/** CUST_ID **/
	private String custId;
	/** TYPE_CDE **/
	private String typeCde;
	/** CTRY_CDE **/
	private Long placeId;
	/** DSTRT **/
	private String dstrt;
	/** ADDR1 **/
	private String addr1;
	/** ADDR2 **/
	private String addr2;
	/** ADDR3 **/
	private String addr3;
	/** ZIP **/
	private String zip;

	private Long countryId;
	private Long provinceId;

	private SelectItem ctryItems[];
	private SelectItem provinceItems[];
	private SelectItem cityItems[];

	public CustomerAddressVo() {
	}

	public void initItems() {
		List<CountryVo> countryList = CrmCache.countryList;
		ctryItems = PlaceUtil.convertCtryItem(countryList);

		if (placeId == null) {
			provinceItems = null;
			cityItems = null;
		} else {
			// find province for city
			PlaceVo city = CrmCache.cityMap.get(placeId);
			if(city == null){
				return;
			}
			setProvinceId(city.getParntPlaceId());
			ProvinceVo province = CrmCache.provinceMap.get(city.getParntPlaceId());
			cityItems = PlaceUtil.convertCityItem(province.getCityList());

			// find country for province
			city = CrmCache.cityMap.get(city.getParntPlaceId());
			if(city == null){
				return;
			}
			setCountryId(city.getParntPlaceId());
			CountryVo country = CrmCache.countryMap.get(city.getParntPlaceId());
			provinceItems = PlaceUtil.convertProvinceItem(country.getProvinceList());
		}
	}

	/**
	 * province drop down list changed
	 * 
	 * @param event
	 */
	public void provinceValueChange(ValueChangeEvent event) {
		Long parentPlaceId = (Long) event.getNewValue();
		if (parentPlaceId == null) {
			cityItems = new SelectItem[] {};
			return;
		}

		ProvinceVo provinceVo = CrmCache.provinceMap.get(parentPlaceId);

		cityItems = PlaceUtil.convertCityItem(provinceVo.getCityList());

	}

	/**
	 * country drop down list changed
	 * 
	 * @param event
	 */
	public void ctryValueChange(ValueChangeEvent event) {

		cityItems = null;

		Long parentPlaceId = (Long) event.getNewValue();
		if (parentPlaceId == null) {
			provinceItems = new SelectItem[] {};
			return;
		}

		CountryVo country = CrmCache.countryMap.get(parentPlaceId);

		provinceItems = PlaceUtil.convertProvinceItem(country.getProvinceList());

	}

	@XmlElement
	public Long getCustAddrId() {
		return custAddrId;
	}

	public void setCustAddrId(Long custAddrId) {
		this.custAddrId = custAddrId;
	}

	@XmlElement
	public String getCustId() {
		return custId;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	@XmlElement
	public String getTypeCde() {
		return typeCde;
	}

	public void setTypeCde(String typeCde) {
		this.typeCde = typeCde;
	}

	@XmlElement
	public String getDstrt() {
		return dstrt;
	}

	public void setDstrt(String dstrt) {
		this.dstrt = dstrt;
	}

	@XmlElement
	public String getAddr1() {
		return addr1;
	}

	public void setAddr1(String addr1) {
		this.addr1 = addr1;
	}

	@XmlElement
	public String getAddr2() {
		return addr2;
	}

	public void setAddr2(String addr2) {
		this.addr2 = addr2;
	}

	@XmlElement
	public String getAddr3() {
		return addr3;
	}

	public void setAddr3(String addr3) {
		this.addr3 = addr3;
	}

	@XmlElement
	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	@XmlElement
	public Long getPlaceId() {
		return placeId;
	}

	public void setPlaceId(Long placeId) {
		this.placeId = placeId;
	}

	public Long getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(Long provinceId) {
		this.provinceId = provinceId;
	}

	public SelectItem[] getCtryItems() {
		return ctryItems;
	}

	public void setCtryItems(SelectItem[] ctryItems) {
		this.ctryItems = ctryItems;
	}

	public SelectItem[] getProvinceItems() {
		return provinceItems;
	}

	public void setProvinceItems(SelectItem[] provinceItems) {
		this.provinceItems = provinceItems;
	}

	public SelectItem[] getCityItems() {
		return cityItems;
	}

	public void setCityItems(SelectItem[] cityItems) {
		this.cityItems = cityItems;
	}

	public Long getCountryId() {
		return countryId;
	}

	public void setCountryId(Long countryId) {
		this.countryId = countryId;
	}

	@Override
	public int compareTo(AuditTrailVo o) {
		CustomerAddressVo vo = (CustomerAddressVo) o;
		if(vo == null){
			//if not set original data, default set to modified.
			return 1;
		}
		String custId = this.getCustId() == null ? "" : this.getCustId();
		String planceId = this.getPlaceId() == null ? "" : this.getPlaceId().toString();
		String typeCde = this.getTypeCde() == null ? "" : this.getTypeCde();
		String addr1 = this.getAddr1() == null ? "" : this.getAddr1();
		String addr2 = this.getAddr2() == null ? "" : this.getAddr2();
		String addr3 = this.getAddr3() == null ? "" : this.getAddr3();
		String district = this.getDstrt() == null ? "" : this.getDstrt();
		String zip = this.getZip() == null ? "" : this.getZip();
		
		String custIdCompareTo = vo.getCustId() == null ? "" : vo.getCustId();
		String planceIdCompareTo = vo.getPlaceId() == null ? "" : vo.getPlaceId().toString();
		String typeCdeCompareTo = vo.getTypeCde() == null ? "" : vo.getTypeCde();
		String addr1CompareTo = vo.getAddr1() == null ? "" : vo.getAddr1();
		String addr2CompareTo = vo.getAddr2() == null ? "" : vo.getAddr2();
		String addr3CompareTo = vo.getAddr3() == null ? "" : vo.getAddr3();
		String districtCompareTo = vo.getDstrt() == null ? "" : vo.getDstrt();
		String zipCompareTo = vo.getZip() == null ? "" : vo.getZip();

		// check if have any field be modified
		int result = 0;
		if (!custId.equals(custIdCompareTo)) {
			result = 1;
		}
		if (!planceId.equals(planceIdCompareTo)) {
			result = 1;
		}
		if (!typeCde.equals(typeCdeCompareTo)) {
			result = 1;
		}
		if (!addr1.equals(addr1CompareTo)) {
			result = 1;
		}
		if (!addr2.equals(addr2CompareTo)) {
			result = 1;
		}
		if (!addr3.equals(addr3CompareTo)) {
			result = 1;
		}
		if (!district.equals(districtCompareTo)) {
			result = 1;
		}
		if (!zip.equals(zipCompareTo)) {
			result = 1;
		}
		return result;
	}
	
}
